package com.dream.dao.impl;

import com.dream.dao.BulletinDao;
import com.dream.pojo.Bulletin;
import com.dream.pojo.BulletinIdInformation;
import com.dream.utils.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class BulletinDaoImpl implements BulletinDao {
    @Override
    public int addBulletin(Bulletin bulletin) {
        return DBUtil.commonInsert("insert into bulletin values(null,?,?,?,0,?,?,?)",
                bulletin.getBulletinTitle(),bulletin.getBulletinImg(),bulletin.getBulletinContent(),
                bulletin.getPublisher(),bulletin.getGmtCreate(),bulletin.getGmtModify());
    }

    @Override
    public List<Bulletin> list() {
        return DBUtil.commonQuery(Bulletin.class,"select * from bulletin");
    }

    @Override
    public Bulletin findById(long bulletinId) {
        ArrayList<Bulletin> bullentins = DBUtil.commonQuery(Bulletin.class,"select * from bulletin where bulletin_id = ?",bulletinId);
        if (bullentins.size()>0){
            return bullentins.get(0);
        }else {
            return null;
        }
    }

    @Override
    public long totalCount(String bulletinTitle, String userName, String startTime, String endTime) {
       String sql = "select count(bulletin.bulletin_id) from bulletin inner join user on bulletin.publisher=user.id where 1=1";
       StringBuilder sb = new StringBuilder(sql);
       if (bulletinTitle!=null && !"".equals(bulletinTitle)){
           sb.append(" and bulletin.bulletin_title like '%\"+bulletinTitle+\"%'");
       }
        if(userName!=null && !"".equals(userName)){
            sb.append(" and user.user_name like '%"+userName+"%'");
        }
        if(startTime!=null && !"".equals(startTime)){
            sb.append(" and bulletin.gmt_create>="+startTime);
        }
        if(endTime!=null && !"".equals(endTime)){
            sb.append(" and bulletin.gmt_modify<="+endTime);
        }
        return DBUtil.queryCount(sb.toString());
    }

    @Override
    public List<BulletinIdInformation> list(String bulletinTitle, String userName, String startTime, String endTime, long pageIndex, long pageSize) {
        String sql = "select bulletin.*,user.user_name from bulletin INNER JOIN user on bulletin.publisher=user.id where 1=1";
        StringBuilder sb = new StringBuilder(sql);
        if(bulletinTitle!=null && !"".equals(bulletinTitle)){
            sb.append(" and bulletin.bulletin_title like '%"+bulletinTitle+"%'");
        }
        if(userName!=null && !"".equals(userName)){
            sb.append(" and user.user_name like '%"+userName+"%'");
        }
        if(startTime!=null && !"".equals(startTime)){
            sb.append(" and bulletin.gmt_create>="+startTime);
        }
        if(endTime!=null && !"".equals(endTime)){
            sb.append(" and bulletin.gmt_modify<="+endTime);
        }
        sb.append(" limit ?,?");
        return DBUtil.commonQuery(BulletinIdInformation.class, sb.toString(),(pageIndex-1)*pageSize,pageSize);
    }

    @Override
    public int updateRedaCount(long bulletinId) {
        try {
        DBUtil.commonUpdate("update bulletin set read_count=read_count+1 where bulletin_id=?",
                bulletinId);
        return 1;
        } catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteById(Long bulletinId) {
        try {
            DBUtil.commonUpdate("delete from bulletin where bulletin_id=?",
                    bulletinId);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateBulletin(Bulletin bulletin) {
        try {
            DBUtil.commonUpdate("update bulletin set bulletin_title=?,bulletin_img=?,bulletin_content=?,publisher=?,gmt_modify=? where bulletin_id=?",
                    bulletin.getBulletinTitle(),
                    bulletin.getBulletinImg(),
                    bulletin.getBulletinContent(),
                    bulletin.getPublisher(),
                    bulletin.getGmtModify(),
                    bulletin.getBulletinId());
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
